package com.guyaogg.gulimall.search.controller;

import com.guyaogg.gulimall.search.service.MallSearchService;
import com.guyaogg.gulimall.search.vo.SearchParam;
import com.guyaogg.gulimall.search.vo.SearchResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 顾遥
 */
@Slf4j
@Controller
public class SearchController {

    private final MallSearchService mallSearchService;

    public SearchController(MallSearchService mallSearchService) {
        this.mallSearchService = mallSearchService;
    }

    @GetMapping("/list.html")
    public String listPage(SearchParam param, Model model, HttpServletRequest request){
        log.debug("接收封装数据：{}",param);

        // 获取 ? 后的查询参数
        String queryString = request.getQueryString();
        param.set_queryString(queryString);
        // 去 es 中检索结果
        SearchResult result =  mallSearchService.search(param);
        model.addAttribute("result", result);
        return "list";
    }
}
